package com.ly.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.BaseUtils;
import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisShopUserMapper;
import com.system.common.pojo.BisShopUser;
import com.system.common.pojo.BisShopUserExample;
import com.system.common.pojo.BisShopUserExample.Criteria;

@Service
public class ShopKeeperServiceImpl implements ShopKeeperService{
   
	@Autowired
	private BisShopUserMapper bisshopusermapper;
	
	
	@Override
	public List<BisShopUser> shoplist(Map<String,Object> map) {
		String account=(String) map.get("account");
		String username=(String) map.get("username");
		String status=(String) map.get("status");
		String mobile=(String) map.get("mobile");
		String idno=(String) map.get("idno");
		String email=(String) map.get("email");
		BisShopUserExample example=new BisShopUserExample();
		Criteria criteria=example.createCriteria();
		
	if (StringUtils.isNotBlank(account)) {
		account="%"+account+"%";
	}
	if (StringUtils.isNotBlank(account)) {
		criteria.andAccountLike(account);
	}
	
	if (StringUtils.isNotBlank(username)) {
		username="%"+username+"%";
	}
	if (StringUtils.isNotBlank(username)) {
		criteria.andUsernameLike(username);
	}
	
	
	if (StringUtils.isNotBlank(status)) {
		criteria.andStatusEqualTo(status);
	}

	
	
	if (StringUtils.isNotBlank(mobile)) {
		mobile="%"+mobile+"%";
	}
	if (StringUtils.isNotBlank(mobile)) {
		criteria.andMobileLike(mobile);
	}
	
	
	if (StringUtils.isNotBlank(idno)) {
		idno="%"+idno+"%";
	}
	if (StringUtils.isNotBlank(idno)) {
		criteria.andIdnoLike(idno);
	}
	
	
	if (StringUtils.isNotBlank(email)) {
		email="%"+email+"%";
	}
	if (StringUtils.isNotBlank(email)) {
		criteria.andEmailLike(email);
	}		
	//创建一个新的list集合
		List<BisShopUser> shopkeep=new ArrayList<>();
		List<BisShopUser> listshop=bisshopusermapper.selectByExample(example);	
		for (BisShopUser bisShopUser : listshop) {			
			if (bisShopUser.getIsDel().equals("0")) {
				shopkeep.add(bisShopUser);
			}
		}
		return shopkeep;
	}


	/**
	 * 添加
	 */
		@Override
		public void add(BisShopUser shopUser) {
			Snowflake sf=new Snowflake(0, 0);
			shopUser.setShopUserId(sf.nextId()+"");
			shopUser.setIsDel("0");
			bisshopusermapper.insert(shopUser);
		}
		/**
		 * 根据主键查询
		 */

		@Override
		public BisShopUser selectshopbyshopuserid(String shopUserId) {
			BisShopUser shopUser=bisshopusermapper.selectByPrimaryKey(shopUserId);
			return shopUser;
		}


		@Override//修改
		public void update(BisShopUser shopUser) {
			shopUser.setIsDel("0");
			bisshopusermapper.updateByPrimaryKey(shopUser);
		}

		@Override//删除
		public void delete(String shopUserId) {
			//获取原先的shopuserid
			BisShopUser shopUser=bisshopusermapper.selectByPrimaryKey(shopUserId);
			//通过调用修改的方法将isdel的数据该为删除的标记(1)
			shopUser.setIsDel("1");
			bisshopusermapper.updateByPrimaryKey(shopUser);
			
		}

//查看详情的员工信息
		@Override
		public List<BisShopUser> queryShopUserByShopId(String shopId, Map<String, Object> map) {
			BisShopUserExample example=new BisShopUserExample();
			Criteria criteria=example.createCriteria();
			criteria.andUserTypeEqualTo("2");
			criteria.andIsDelEqualTo("0");
			criteria.andShopIdEqualTo(shopId);
			
			String account = (String) map.get("baccount");
			String postCode = (String) map.get("bpostName");
			String status =(String) map.get("status");
			String username = (String) map.get("busername");
			String workNum = (String) map.get("bworkNum");
			String mobile = (String) map.get("bmobile");
			String idno = (String) map.get("bidno");
			String shopid=(String) map.get("bshopName");
			
			if (StringUtils.isNotBlank(shopid)) {
				criteria.andShopIdLike("%"+shopId+"%");
			}
			if (StringUtils.isNotBlank(account)) {
				criteria.andShopIdLike("%"+account+"%");
			}
			if (StringUtils.isNotBlank(postCode)) {
				criteria.andShopIdLike("%"+postCode+"%");
			}
			if (StringUtils.isNotBlank(status)) {
				criteria.andStatusEqualTo(status);
			}
			if (StringUtils.isNotBlank(username)) {
				criteria.andUsernameLike("%"+username+"%");
			}
			if (StringUtils.isNotBlank(workNum)) {
				criteria.andWorkNumberEqualTo(workNum);
			}
			if (StringUtils.isNotBlank(mobile)) {
				criteria.andShopIdLike("%"+mobile+"%");
			}
			if (StringUtils.isNotBlank(idno)) {
				criteria.andIdnoEqualTo(idno);
			}
			List<BisShopUser> bisShopUserlist = bisshopusermapper.selectByExample(example);
			for (BisShopUser bisShopUser : bisShopUserlist) {
				bisShopUser.setPassword(BaseUtils.decrypt(bisShopUser.getPassword(), BaseUtils.PASSWORD_KEY));
			}
			return bisShopUserlist;
			
		}

	
}
